package com.five.calendar.database.observer;

import com.five.calendar.common.observer.Observable;
import com.five.calendar.database.DataProvider;
import com.five.calendar.database.bean.TargetBean;

import java.util.List;

/**
 * Created by xin on 2017/8/31.
 */

public class TargetObservable extends Observable<TargetBean, Integer, List<TargetBean>> {
    private static final int FLAG_SAVE_TARGET = 1;

    private static final int FLAG_DELETE_TARGET = 2;

    private static final int FLAG_QUERY_TARGET = 3;

    private static final int FLAG_UPDATE_TARGET = 4;

    private static final int FLAG_QUERY_TARGET_BY_TIME = 5;

    private int mFlag;

    private String mTitle;

    private Long mStartTime;

    private Long mEndTime;

    private String mDetail;

    private String mType;

    private int mColor;

    private int isComplete;

    private TargetBean mTargetBean;

    private List<TargetBean> mList;

    private Long mQueryEndTime;

    private String mReminderText;

    public TargetObservable(int mFlag) {
        this.mFlag = mFlag;
    }

    public TargetObservable(int mFlag, TargetBean mTargetBean) {
        this.mFlag = mFlag;
        this.mTargetBean = mTargetBean;
    }

    private TargetObservable(int mFlag, List<TargetBean> mList) {
        this.mFlag = mFlag;
        this.mList = mList;
    }

    private TargetObservable(int mFlag, String mTitle, Long mStartTime, Long mEndTime, String mDetail, String mType, int mColor, String mReminderText, int isComplete) {
        this.mFlag = mFlag;
        this.mTitle = mTitle;
        this.mStartTime = mStartTime;
        this.mEndTime = mEndTime;
        this.mDetail = mDetail;
        this.mType = mType;
        this.mColor = mColor;
        this.mReminderText = mReminderText;
        this.isComplete = isComplete;
    }

    public TargetObservable(int mFlag, long endtime) {
        this.mFlag = mFlag;
        this.mQueryEndTime = endtime;
    }

    public static TargetObservable queryTarget() {
        return new TargetObservable(FLAG_QUERY_TARGET);
    }

    public static TargetObservable queryTargetByTime(long endtime) {
        return new TargetObservable(FLAG_QUERY_TARGET_BY_TIME, endtime);
    }

    public static TargetObservable deleteTarget(List<TargetBean> mList) {
        return new TargetObservable(FLAG_DELETE_TARGET, mList);
    }

    public static TargetObservable updateTarget(TargetBean mTargetBean) {
        return new TargetObservable(FLAG_UPDATE_TARGET, mTargetBean);
    }

    public static TargetObservable saveTarget(String mTitle, Long mStartTime, Long mEndTime, String mDetail, String mType, int mColor, String mReminderText, int isComplete) {
        return new TargetObservable(FLAG_SAVE_TARGET, mTitle, mStartTime, mEndTime, mDetail, mType, mColor, mReminderText, isComplete);
    }

    @Override
    public void run() {
        switch (mFlag) {
            case FLAG_SAVE_TARGET:
                TargetBean targetBean = DataProvider.getInstance().insertTargetBean(mTitle, mStartTime, mEndTime, mDetail, mType, mColor, mReminderText, isComplete);
                if (null != targetBean) {
                    handleSuccess(targetBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
            case FLAG_DELETE_TARGET:
                if (DataProvider.getInstance().deleteTargetBean(mList)) {
                    for (TargetBean bean : mList) {
                        handleSuccess(bean);
                    }
                } else {
                    handleFail(-1);
                }
                handleComplete(mList);
                break;
            case FLAG_QUERY_TARGET:
                handleComplete(DataProvider.getInstance().queryTargetBean());
                break;
            case FLAG_UPDATE_TARGET:
                if (DataProvider.getInstance().updateTargetBean(mTargetBean)) {
                    handleSuccess(mTargetBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
            case FLAG_QUERY_TARGET_BY_TIME:
                handleComplete(DataProvider.getInstance().queryTargetBeanForTime(mQueryEndTime));
                break;
        }
    }
}
